package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.entity.jz.AppBanner;
import com.sxbbc.common.core.enums.BannerPositionEnums;
import com.sxbbc.common.core.enums.EventEnums;
import com.sxbbc.common.core.mapper.jz.AppBannerMapper;
import com.sxbbc.common.core.service.IAppBannerService;
import com.sxbbc.common.core.utils.BasePage;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * APP banner图 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-14
 */
@Service
public class AppBannerServiceImpl extends ServiceImpl<AppBannerMapper, AppBanner> implements IAppBannerService {

    @Override
    public Page<AppBanner> queryAppIconList(BasePage<AppBanner> page, AppBanner banner) {
        QueryWrapper<AppBanner> wrapper = queryWrapper(banner);
        if (page.isPaging()) {
            super.page(page, wrapper);
        } else {
            List<AppBanner> list = super.list(wrapper);
            page.setRecords(list);
        }
        for (AppBanner b : page.getRecords()) {
            b.setEventName(EventEnums.getName(b.getEvent()));
            b.setBannerPosition(BannerPositionEnums.getName(b.getType()));
        }
        return page;
    }

    @Override
    public List<AppBanner> queryBannerListForApi(AppBanner appBanner) {
        QueryWrapper<AppBanner> wrapper = new QueryWrapper<>();
        if (appBanner.getPosition() != null) {
            wrapper.eq("position",appBanner.getPosition());
        }
        if (appBanner.getType() != null) {
            wrapper.eq("type",appBanner.getType());
        }
        return super.list(wrapper);
    }

    private QueryWrapper<AppBanner> queryWrapper(AppBanner banner) {
        QueryWrapper<AppBanner> wrapper = new QueryWrapper<>();
        if (banner.getPosition() != null) {
            wrapper.eq("position", banner.getPosition());
        }
        if (banner.getType() != null) {
            wrapper.eq("type", banner.getType());
        }
        // api端如果设置orderBy标记进行操作
        if (banner.isOrderFlag()) {
            wrapper.orderByDesc("sort");
        }
        return wrapper;
    }
}
